<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 商品分类
                </el-breadcrumb-item>
                <el-button style="margin-left: 90%"
                           type="text"
                           icon="el-icon-refresh"
                           @click="getData"
                >刷新
                </el-button>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除
                </el-button>

                <el-input size="small" style="margin-right: 5px;width: 16%" prefix-icon="el-icon-search"
                          v-model="query.categoryName" placeholder="请输入分类名称" class="handle-input mr10"></el-input>

                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-circle-plus" @click="handleAdd">添加分类</el-button>
            </div>
            <el-table
                :data="CategoryTable"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                width="100%"
                @selection-change="handleSelectionChange">

                <el-table-column type="selection" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="70%" align="center">
                    <template slot-scope="scope">
                        {{ scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column prop="pid" label="分类级别" align="center">
                    <template slot-scope="scope">{{ scope.row.pid === 0 ? '一级分类' : '分类未知' }}</template>
                </el-table-column>
                <el-table-column prop="categoryName" label="分类名称" align="center"></el-table-column>

                <el-table-column prop="addTime" label="添加时间" align="center"></el-table-column>
                <el-table-column prop="updTime" label="最后更新时间" align="center"></el-table-column>
                <el-table-column label="操作" width="160%" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.row.id)"
                        >编辑
                        </el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.row.id)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!--         添加弹窗 -->
        <el-dialog title="添加分类" :visible.sync="addVisible" width="30%">
            <el-form :model="category">
                <el-form-item label="分类级别">
                    <el-select v-model="category.pid" placeholder="请选择" style="width: 50%">
                        <el-option key="1" label="一级分类" :value="0"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="分类名称">
                    <el-input @keyup.enter.native="saveCategory" @keyup.esc.native="saveCategory"
                              v-model="category.categoryName" style="width: 50%"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel()">取 消</el-button>
                <el-button type="primary" @click="saveCategory()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
    delProductCategory,
    getProductCategoryInfo,
    getLisProductCategory,
    addUpdateProductCategory
} from '@/api/productCategory';


export default {
    name: 'basetable',
    data() {
        return {
            query: {
                pageIndex: 1,
                pageSize: 10
            }, //查询条件
            CategoryTable: [],
            multipleSelection: [], //多选参数
            addVisible: false,
            pageTotal: 0,
            category: {},
            ids: {
                ids: []
            }
        };
    },
    created() {
        this.getData();
    },
    methods: {

        //编辑弹窗
        handleEdit(id) {
            getProductCategoryInfo(id).then(res => {
                if (res.success) {
                    this.addVisible = true;
                    this.category = res.data;
                    console.log(this.attendanceDetailsData);
                } else {
                    this.$message.error(res.message);

                }
            });

        },

        //保存
        saveCategory() {
            addUpdateProductCategory(this.category).then(res => {
                console.log(res);
                if (res.success) {
                    this.$message.success('添加成功');
                    this.addVisible = false;
                    this.getData();
                } else {
                    this.$message.warning(res.message);
                }
            });


        },

        //打开添加弹窗
        handleAdd() {
            this.category = {};
            this.addVisible = true;
        },
        // 获取列表
        getData() {
            getLisProductCategory(this.query).then(res => {
                if (res.success) {
                    this.CategoryTable = res.data.records;
                    this.pageTotal = res.data.total || 0;
                } else {
                    this.$message.warning(res.message);
                    if (res.code === 10005) {
                        this.$router.push({ path: '/login' });
                    }
                }

            });
        },
        //取消按钮
        cancel() {
            this.addVisible = false;
            //清空对象
            this.category = {};
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(id) {
            if (id !== undefined) {
                this.ids.ids = [];
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                this.ids.ids.push(id);
                delProductCategory(this.ids).then(res => {
                    if (res.success) {
                        this.$message.success('删除成功');
                        this.getData();
                    } else {
                        this.$message.error(res.message);
                    }

                });
            }).catch(() => {
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        //多选删除
        delAllSelection() {
            for (let i = 0; i < this.multipleSelection.length; i++) {
                this.ids.ids.push(this.multipleSelection[i].id);

            }
            this.handleDelete();

        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>

.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}

.everyDay {
    display: inline-block;
    width: 20px;
    height: 20px;
    background-color: #409eff;
    color: #fff;
    border-radius: 50%;
}
</style>
